package com.nervenets.web.controller.handler.admin.setting;

import com.nervenets.web.controller.handler.SessionHandler;
import com.nervenets.web.enumeration.Role;
import com.nervenets.web.hibernate.domain.setting.Help;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class HelpListHandler extends SessionHandler<HelpListParams> {
    @Override
    public ArrayList<Role> allowRoles() {
        return Role.administrator();
    }

    @Override
    public boolean needLog() {
        return false;
    }

    @Override
    public String handle(HelpListParams params, HttpServletRequest request) throws Exception {
        String hql = "from Help ";
        List<Object> objects = new ArrayList<>();

        if (params.getTypeId() > 0) {
            hql += "where typeId = ? ";
            objects.add(params.getTypeId());
        }

        BigDecimal countOfByHql = userDao.getCountOfByHql(hql, objects);
        hql += "order by queue desc ";
        List<Help> helps = userDao.getAllObjectsByHql(hql, objects, params.getStart(), params.getLimit());
        return successMessage(helps,
                new KeyValue("start", params.getStart()),
                new KeyValue("total", countOfByHql.intValue()));
    }

    @Override
    public String result() throws Exception {
        return null;
    }
}
